<template>
	<view class="my-index flex-jc-ac" :style="{opacity:opacity}">
		<text @click="getUser">进入app</text>
	</view>
</template>

<script>
	const openinstall = uni.requireNativePlugin('openinstall-plugin');
	import {logins} from "@/utils/common.js"
	import {apiGetView,apiGetUserInstallUrl} from "@/api/me.js"
	import {version, versionCompare} from "@/api/login.js"
	export default{
		data(){
			return{
				opacity:0,
				openinstall: openinstall
			}
		},
		onLoad() {
			this.getUser()
			
		},
		methods:{
			versions(){
				// 获取版本信息
				version({
					type: uni.getSystemInfoSync().platform == 'android' ? 1 : 2
				}).then(r => {
					if (versionCompare(r.data.version, plus.runtime.version)) {
						var temp = encodeURI(r.data.upload_apk.path_text)
						// this.opacity = 1
						// 查看是否强制更新
						if (r.data.status == 1) {
							uni.showModal({
								title: "更新提示",  
								content: "你的版本过低", 
								showCancel:false,
								success: (e) => {
									plus.runtime.openURL(uni.getStorageSync("installUrl"))
									// plus.runtime.quit();  强制退出APP
								}
							})  
						}else {
							uni.showModal({ //提醒用户更新
								title: "更新提示",  
								content: "你的版本过低",  
								success: (e) => {
									if (e.confirm) {  
										// 手机必须要安装浏览器才可以
										plus.runtime.openURL(uni.getStorageSync("installUrl"))  //打开web下载安装
									} else {
										this.goHome()
									} 
								}  
							})  
						}
					}else{
						this.goHome()
					}
				}).catch(e => {
					this.goHome()
				})	
			},
			getUser(){
				// 获取用户信息
				apiGetView().then(resData => {
					uni.setStorageSync("userinfo",resData.data)
					this.versions()
				}).catch((e)=>{
					uni.setStorageSync("logins",true)
					// 获取安装参数中
					this.handleOpenInstall().then(()=>{
						// 登录中
						logins(this.goHome,this.versions)
					})
				})
			},
			handleOpenInstall(){
				// openinstall
				return new Promise((resfn,fejfn)=>{
					openinstall.getInstall(
					    8,
					    function(result) {
							uni.setStorageSync("openInstallValue",result.bindData)
							resfn()
					    }
					)
				}) 
			},
			goHome(){
				setTimeout(()=>{
					uni.switchTab({
						url:"/pages/home/home"
					})
				},300)
				// 获取openstall静态页面地址
				apiGetUserInstallUrl().then(res=>{
					uni.setStorageSync("installUrl",res.data)
				})
			}
		}
	}
</script>
<style>
.my-index{
	font-size: 80upx;
}
</style>
